package com.hua.api.modules.order.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.hua.api.core.constants.Status;
import com.hua.api.core.framework.statement.EntityManagerTemplate;
import com.hua.api.core.framework.statement.Statement;
import com.hua.api.core.framework.statement.mysql.StatementFactoryImpl;
import com.hua.api.modules.order.domain.OrderBid;
import com.hua.api.modules.order.domain.OrderInfo;
import com.hua.api.modules.order.domain.OrderItems;
import com.hua.api.modules.pay.domain.OrderInfoPayinfo;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class OrderInfoDao extends EntityManagerTemplate {

    public OrderInfo findOrderInfoWithOrderNo(String orderNo){
        Statement stms = StatementFactoryImpl.getInstance().createStatement(" from OrderInfo o where o.orderNo=?1");
        stms.addParam(orderNo);
        return findObject(stms,OrderInfo.class);
    }


    public List<OrderItems> queryOrderItems(Integer orderId){
        Statement stms = StatementFactoryImpl.getInstance().createStatement(" from OrderItems oi where oi.order.id=?1");
        stms.addParam(orderId);
        return query(stms,OrderItems.class);
    }


    // 查询订单支付信息
    public OrderInfoPayinfo findPayInfo(String orderNo){

        Statement statement = StatementFactoryImpl.getInstance().createStatement(" from OrderInfoPayinfo p where p.orderNo=?1 and p.state=?2");
        return null;
    }

    public Long queryDaySeqWithSource(String source){
        Statement stms = StatementFactoryImpl.getInstance().createNativeStatement("SELECT count(1) FROM order_info o WHERE o.source=?1 and o.ctime>=?2");
        stms.addParams(source, DateUtil.beginOfDay(new Date()).toJdkDate());
        return findCount(stms);
    }


    // 设置需要设置完成订单
    public List<OrderInfo> queryNeedSetToDoneOrderInfo(Date endDate){
        Statement statement = StatementFactoryImpl.getInstance().createStatement("from OrderInfo o where o.utime<=?1 and o.status=?2 and o.logisticsStatus=?3 ");
        statement.addParams(endDate, Status.OrderStatus.yijiedan.getValue(),Status.LogisticsStatus.yisongda.getValue());
        statement.addOrderBy( " o.utime asc");
        return query(statement,100,OrderInfo.class);
    }



    public List<OrderInfo> queryNeedBillOrderInfo(Date endDate){
        Statement statement = StatementFactoryImpl.getInstance().createStatement("from OrderInfo o where o.utime<=?1 and o.status=?2 and o.bill=?3 ");
                  statement.addParams(endDate, Status.OrderStatus.wancheng.getValue(),Status.BillMode.bill_no.getValue());
                  statement.addOrderBy( " o.utime asc");
        return query(statement,100,OrderInfo.class);
    }

    // 查询需要计算配送距离的订单信息。
    public List<OrderInfo> queryNeedDistanceFixed(Date beginDate,Integer ignoreShopId){
        Statement statement = StatementFactoryImpl.getInstance().createStatement("from OrderInfo o where o.ctime>=?1 and o.status=?2 and o.logisticsStatus=?3 and o.distance=0 ");
        statement.addParams(beginDate, Status.OrderStatus.yijiedan.getValue(),Status.LogisticsStatus.daipeisong.getValue());
        statement.append("and","o.shopId","!=",ignoreShopId);
        statement.addOrderBy( " o.id asc");
        return query(statement,100,OrderInfo.class);
    }

}
